package pe.tp2.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pe.tp2.beans.DetalleEmbarque;
import pe.tp2.beans.DetalleParametro;
import pe.tp2.beans.Embarque;
import pe.tp2.dao.implementacion.ITenderDAO;
import pe.tp2.dao.implementacion.ImplTenderDAO;
import pe.tp2.dao.service.TenderDAOService;
import pe.tp2.dao.service.TodoBusinessDelegate;
import pe.tp2.util.StringUtil;

/**
 * Servlet implementation class SRegistrarEvaluacion
 */
public class SRegistrarEvaluacion extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SRegistrarEvaluacion() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procesarLlamada(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procesarLlamada(request, response);
	}

	private void procesarLlamada(HttpServletRequest request,
			HttpServletResponse response) {
		String accion = request.getParameter("accion");
		String pagina = "";
		
		
		try {
			if(accion.equals("mostrarBusqueda")){
				pagina = mostrarBusqueda(request, response);
			}else if(accion.equals("buscar")){
				pagina = buscar(request, response);
			}else if(accion.equals("verEvaluacion")){
				pagina = verEvaluacion(request, response);
			}else if(accion.equals("verParametro")){
				pagina = verParametro(request, response);
			}else if(accion.equals("verCaracterisica")){
				pagina = verCaracterisica(request, response);
			}else if(accion.equals("grabar")){
				pagina = grabar(request, response);
			}
			response.sendRedirect(pagina);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String grabar(HttpServletRequest request, HttpServletResponse response) {
		try {
			
			TenderDAOService tenderService = TodoBusinessDelegate.getTenderService();
			String id = request.getParameter("id");
			String estado = request.getParameter("estado");
			
			Embarque objEmbarque = new Embarque(); 
			objEmbarque.setId(Integer.parseInt(id));
			objEmbarque.setEstado(Integer.parseInt(estado));
			 
			tenderService.actualizarEmbarque(objEmbarque);
			
			request.getSession().removeAttribute("lstEmbarques");
			request.getSession().removeAttribute("objEmbarque");
					
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "pages/realizarEvaluacion.jsp";
	}

	private String verCaracterisica(HttpServletRequest request, HttpServletResponse response) {
		String idDetalle = request.getParameter("idDetalle");
		System.out.println("idDetalle:"+idDetalle);
		
		return "pages/inc/inc_detalleCaracteristica.jsp?idDetalle="+idDetalle;
	}

	private String verParametro(HttpServletRequest request, HttpServletResponse response) {
		String idDetalle = request.getParameter("idDetalle");
		System.out.println("idDetalle:"+idDetalle);
		
		Embarque objEmbarque = (Embarque) request.getSession().getAttribute("objEmbarque");
		DetalleEmbarque objDetalleEmbarque = new DetalleEmbarque();
		
		for(DetalleEmbarque det : objEmbarque.getDetalleEmbarque()){
			if(det.getId() == Integer.parseInt(idDetalle)){
				objDetalleEmbarque = det;
				break;
			}
		}
		request.getSession().setAttribute("objDetalleEmbarque", objDetalleEmbarque);
		
		return "pages/inc/inc_detalleParametro.jsp?idDetalle="+idDetalle;
	}

	private String verEvaluacion(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getSession().removeAttribute("objEmbarque");
			TenderDAOService tenderService = TodoBusinessDelegate.getTenderService();
			ArrayList<Embarque> lstEmbarque = (ArrayList<Embarque>) request.getSession().getAttribute("lstEmbarque");
			String id = request.getParameter("id");
			Embarque objEmbarque = new Embarque();
			
			for(Embarque e : lstEmbarque){
				if(e.getId() == Integer.parseInt(id)){
					objEmbarque = e;
					break;
				}
			}
			
			objEmbarque.setDetalleEmbarque(tenderService.buscarDetalleEmbarque(objEmbarque));
			
			for(DetalleEmbarque det : objEmbarque.getDetalleEmbarque()){
				det.setParametros(tenderService.buscarDetalleParametro(det.getId_brand()));
				/*for(DetalleParametro par : det.getParametros()){
					par.setParticipante(det.getParticipante());
				}*/
			}
			
			ArrayList<DetalleEmbarque> participantes = new ArrayList<DetalleEmbarque>();
			
			boolean existe = false;
			for(DetalleEmbarque det : objEmbarque.getDetalleEmbarque()){
				for(DetalleEmbarque i : participantes){
					if(i.getId_participante() == det.getId_participante()){
						existe = true;break;
					}
				}
				if(existe == false){
					participantes.add(det);
				}
				existe = false;
			}
			objEmbarque.setParticipantes(participantes);
			
			request.getSession().setAttribute("objEmbarque", objEmbarque);
			System.out.println("id:"+id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "pages/inc/inc_detalleEmbarque.jsp";
	}

	private String buscar(HttpServletRequest request, HttpServletResponse response) {
		TenderDAOService tenderService = TodoBusinessDelegate.getTenderService();
		request.getSession().removeAttribute("lstEmbarque");
		request.getSession().removeAttribute("objEmbarque");
		Embarque objEmbarque = new Embarque();
		
		try {
			
			objEmbarque.setDescripcion(StringUtil.noNullString(request.getParameter("embarque")));
			objEmbarque.setEstado(Integer.parseInt(StringUtil.noNullString(request.getParameter("estado"))));
			objEmbarque.setTenderDescrip(StringUtil.noNullString(request.getParameter("codigoTender")));
			
			ArrayList<Embarque> lstEmbarque = tenderService.buscarEmbarques(objEmbarque);
			request.getSession().setAttribute("lstEmbarque", lstEmbarque);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		return "pages/realizarEvaluacion.jsp";
	}

	private String mostrarBusqueda(HttpServletRequest request, HttpServletResponse response){
		
		request.getSession().removeAttribute("lstEmbarques");
		request.getSession().removeAttribute("objEmbarque");
				
		return "pages/realizarEvaluacion.jsp";
	}

}
